/**
 * 
 */
package com.novartis.nims.imagedc.logic;

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.core.PersistenceException;
import org.simpleframework.xml.core.Validate;

import com.pixelmed.dicom.AttributeTag;

/**
 * This class abstracts the notion of a DICOM attribute tag. It's a lightweight
 * delegate for {@link com.pixelmed.dicom.AttributeTag}. The only additional
 * functionality it provides is initialization using hexadecimal string
 * representation of the tag's group and element values. This functionality can
 * be leveraged to allow instantiation and initialization via deserialization
 * using the Simple framework.
 * 
 * @author Josh Snyder
 * @see org.simpleframework.xml
 */
public class AttributeTagDelegate {

	@Attribute(name="Group")
	private String group = null;

	@Attribute(name="Element")
	private String element = null;
	
	private AttributeTag tag = null;

	/**
	 *  no argument constructor needed for Simple serialization framework
	 *  private so that nobody else can use it to instantiate this class w/out
	 *  initializing its fields
	 */
	@SuppressWarnings("unused")
	private AttributeTagDelegate() {}
	
	/**
	 * Constructs the AttributeTagDelegate using hexadecimal string
	 * representations of the group and element integers. Validates the
	 * parameters and throws a <code>NumberFormatException</code> on parameters
	 * which aren't hexadecimal strings.
	 * 
	 * @param theGroup
	 * @param theElement
	 * @throws NumberFormatException
	 */
	public AttributeTagDelegate(String theGroup, String theElement) throws NumberFormatException {
		validateAndInitialize(theGroup, theElement);
	}
	
	/**
	 * Constructs the AttributeTagDelegate using integers to initialize the
	 * group and element.
	 * 
	 * @param theGroup
	 * @param theElement
	 */
	public AttributeTagDelegate(int theGroup, int theElement) {
		tag = new AttributeTag(theGroup, theElement);
	}
	
	/**
	 * Constructs the delegate using a {@link com.pixelmed.dicom.AttributeTag}
	 * instance to initizialize the underlying object.
	 * 
	 * @param theTag
	 */
	public AttributeTagDelegate(AttributeTag theTag) {
		tag = theTag;
	}
	
	private void validateAndInitialize(String theGroup, String theElement) throws NumberFormatException {
		tag = new AttributeTag(Integer.parseInt(theGroup, 16), Integer.parseInt(theElement, 16));
	}
	
	@SuppressWarnings("unused")
	@Validate
	private void validate() throws PersistenceException {
		try {
			validateAndInitialize(group, element);
		} catch (NumberFormatException e) {
			throw new PersistenceException(e, "Invalid group or element has been specified.");
		}
	}
	
	public int getGroup() {
		return tag.getGroup();
	}

	public int getElement() {
		return tag.getElement();
	}
	
	@Override
	public boolean equals(Object o) {
		if (o instanceof AttributeTagDelegate) {
			return tag.equals(((AttributeTagDelegate)o).tag);
		} else {
			return false;
		}
	}

	@Override
	public int hashCode() {
		return tag.hashCode();
	}

	@Override
	public String toString() {
		return tag.toString();
	}
	
	public AttributeTag getAttributeTag() {
		return tag;
	}

}
